package com.dropdb.demo.controller.wx;

import com.dropdb.demo.bean.Topic;
import com.dropdb.demo.bean.vo.BaseRespVO;
import com.dropdb.demo.service.TopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("wx/topic")
public class WxTopicController {
    @Autowired
    TopicService topicService;


    @RequestMapping(value = "list", method = RequestMethod.GET)
    public BaseRespVO listTopics(Integer page, Integer size) {
        HashMap<String, Object> topics = topicService.listTopics(page, size);
        if (topics == null) return BaseRespVO.error(500, "专题信息不存在");
        return BaseRespVO.ok(topics);
    }


    @RequestMapping(value = "detail", method = RequestMethod.GET)
    public BaseRespVO getTopicDetail(Integer id) {
        HashMap<String, Object> topicDetail = topicService.getTopicDetail(id);
        if (topicDetail == null) return BaseRespVO.error(500, "专题信息不存在");
        return BaseRespVO.ok(topicDetail);
    }


    /**
     * 由于topic表中除了没什么字段表示topic的关系，
     * 而且goods字段除非用数据库，不然没法添加
     * 所以只能用sort_order字段
     * @return
     */
    @RequestMapping(value = "related", method = RequestMethod.GET)
    public BaseRespVO getRelatedTopic(Integer id) {
        List<Topic> relatedTopics = topicService.getRelatedTopic(id);
        return BaseRespVO.ok(relatedTopics);
    }
}
